<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
    <meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
    <title></title>
    <script src="js/mui.min.js"></script>
    <link href="css/mui.min.css" rel="stylesheet"/>
    <link href="style/fjol_style.css" rel="stylesheet" />
    <link rel="stylesheet" type="text/css" href="style/xol_style.css"/>
    <script type="text/javascript" charset="utf-8">
      	mui.init();
    	
   		
    </script>
	</head>
	<body>
		<header class="mui-bar mui-bar-nav newbgRed">
		    <a class="mui-action-back mui-icon mui-icon-left-nav mui-pull-left mr10"></a>
		    <div class="mui-clearfix mui-pull-left pct80 mt5">
		        <div class="mui-input-row fjol-search" style="background: white;">
		        	<span class="mui-icon mui-icon-search" id="searchIcon" style="padding: 0 5px;"></span>
	            	<input type="text" class="mui-input-clear p5" placeholder="搜索房源" id="searchInput" style="width: 88%; height: 30px;">
		        </div>
		        <!--<span class="mui-pull-right color9" style="vertical-align: middle; margin-top: 0.6em;" onclick="mui.back()">取消</span>-->
		    </div>
	        	<!--<span class="mui-pull-right color02b260 pt10 mr10" id="searchIcon">搜索</span>-->
	        
		</header>
		
		<div class="mui-content">
		    
		    <div class="mui-scroll-wrapper" style="top: 74px;">
		        <div class="mui-scroll rel">
		            <!--这里放置真实显示的DOM内容--> 
			    	<ul class="mui-table-view" id="searchStorage">
			            <!--<li class="mui-table-view-cell">
			                <a class="">
			                    Item 1
			                </a>
			            </li>-->
			        </ul>
		            <p class="mui-text-center" style="line-height: 4em;" id="clearStorage">清除搜索历史</p>
		            <ul class="mui-table-view" id="buildingNameList" style="position: absolute; top: 0px; width: 100%; z-index: 33;">
		            	
		            </ul>
		        </div>
		    </div>
		   
		</div>
		<script src="js/jquery-2.1.0.js"></script>
		<script src="js/jquery.tmpl.min.js"></script>
		<script src="js/generalUtil.js"></script>
		<script src="js/fj_area_util.js"></script>
		<script src="js/md5.js"></script>
		<script src="js/common.util.js"></script>
		<script src="js/network.js"></script>
		<script src="js/xzx.open.js"></script>
		
		<script id="buildingNameTmpl" type="text/x-jquery-tmpl">
			<li class="mui-table-view-cell" id="${id}">
                <span>${name}</span>
                <p>${address}</p>
            </li>
		</script>
		<script>
			mui('.mui-scroll-wrapper').scroll({
			deceleration: 0.0005 //flick 减速系数，系数越大，滚动速度越慢，滚动距离越小，默认值0.0006
		});
		var storageArr = [];
		var res=[];
		mui.plusReady(function(){
			if(common.myStorage.getItem('searchList')){
				storageArr = common.myStorage.getItem('searchList');
			}
			var wv = plus.webview.currentWebview();
			var parentName = wv.parentName;
			
			document.getElementById("searchInput").addEventListener("keyup",function(e){ 
		        
		        var searchInput = this.value;
		        var page = 1;
		        var rows = 5;
		        getBuildingName(searchInput,page,rows);
		        
		    },false); 
			document.getElementById("searchInput").addEventListener("keydown",function(e){ 
		        if(13 == e.keyCode){ //点击了“搜索”  
		           document.activeElement.blur();//隐藏软键盘 
		           var searchInput = $('#searchInput').val();
//		           console.log(searchInput);
		           searchFn(searchInput);
		        } 
		    },false); 
    
			document.querySelector('#searchIcon').addEventListener('tap',function(){
				var searchInput = $('#searchInput').val();
				
//				console.log(parentName);
				searchFn(searchInput);
			});
			
			var searchStorageId = document.querySelector('#searchStorage');
			var searchStorageArr = [];
			var searchStorage = common.myStorage.getItem('searchList');
			
			if(searchStorage){
//				console.log(searchStorage.length);
				var res = [];
				if(searchStorage.length>10){
					for (var i=0; i<10; i++) {
						res[i] = storageArr[i+1];
					}
					storageArr=res;
					searchStorage=res;
//					console.log(searchStorage.length);
				}
				for (var i=searchStorage.length-1; i>=0; i-- ) {
					if(searchStorage[i]==''){
						
					}else{
						searchStorageArr.push('<li class="mui-table-view-cell">'+searchStorage[i]+'</li>');
					}
					
				}
				searchStorageId.innerHTML = searchStorageArr.join('');
			}else{
//				console.log(2);
				searchStorageId.innerHTML = '<li class="mui-table-view-cell">暂无历史记录</li>';
			}
			
			//清除缓存
			document.querySelector('#clearStorage').addEventListener('tap',function(){
				 common.myStorage.removeItem('searchList');
				 searchStorageId.innerHTML = '<li class="mui-table-view-cell">暂无历史记录</li>';
				 location.reload();
			});
//			console.log(parentName);
			mui('#searchStorage').on('tap','li',function(){
				var chooseSearchCon = $(this).text();
				parentNameFn(parentName,chooseSearchCon);
			});
			
			
				/*选择搜索词，插入搜索历史，并返回父级页面*/
				mui('#buildingNameList').on('tap','li',function(){
//					console.log(parentName);
					var chooseBuildingName = this.firstChild.nextSibling.innerHTML;		//第一个子节点是'换行'
					searchFn(chooseBuildingName);
					
				});
		});
	
		
function searchFn(searchInput){
	var wv = plus.webview.currentWebview();
	var parentName = wv.parentName;
//	if(searchInput==''){
//					mui.alert('请输入您想要的小区或地址');
//					return false;
//				}else{
					parentNameFn(parentName,searchInput);
//				}
				res = storageArr;
				storageArr.push(searchInput);
					for (var i=0; i<storageArr.length; i++) {
						if(searchInput==res[i]){
							if(i==storageArr.length-1){
								break;
							}else{
								storageArr.pop();
//								console.log(searchInput);
								break;
							}
							
						}
					}
				
				common.myStorage.setItem('searchList',storageArr);
}

/*判断各自父级页面来的情况*/
function parentNameFn(parentName,searchInput){
	if(parentName == 'houseList'){
					var parentId = plus.webview.getWebviewById('fj_house_list');
					mui.fire(parentId,'getSearchkey',{
						searchWord:searchInput
					});
					var parentPage = plus.webview.getWebviewById('fj_house_list');
					mui.back();
			}else if(parentName == 'second_searchList'){
					var parentId = plus.webview.getWebviewById('secondhand_house');
					mui.fire(parentId,'getSearchkey',{
						searchWord:searchInput
					});
//	console.log(parentName);
					mui.back();
				}else if(parentName == 'newHouse_searchList'){
					var parentId = plus.webview.getWebviewById('new_house');
					mui.fire(parentId,'getSearchkey',{
						searchWord:searchInput
					});
					mui.back();
				}else if(parentName == 'indexPage'){
					openWindowWithPage('secondhand_house',{
						searchWord:searchInput
					});
				}
}

			/*搜索楼盘名称*/
			function getBuildingName(searchValue,pagevalue,row){
				var param = [{
					key:'searchKey',
					value:searchValue
				},{
					key:'page',
					value:pagevalue
				},{
					key:'rows',
					value:row
				}];
				fjNetwork.get('fjzx/common/searchBuilding.json',param,function(data){
//					console.log(JSON.stringify(data));
					var rows = data.rows;
					$('#buildingNameTmpl').tmpl(rows).appendTo('#buildingNameList');
				},function(msg){
					mui.toast(msg);
				});
			}
		</script>
		<script src="js/fj_public_js.js"></script>
	</body>
</html>
